const { Controller } = require('egg')
const indexRule = {
	pageSize: 'string',
	pageNum: 'string',
}
const updateRule = {
	status: 'number',
}

/**
 * @controller Audit 文章审核列表
 */
class AuditController extends Controller {
	/**
	 * @summary 文章审核列表
	 * @description 文章审核列表
	 * @router get /admin/audit
	 * @request query number pageSize eg:10 页尺寸
	 * @request query number pageNum eg:1 页码
	 */
	async index() {
		const ctx = this.ctx
		try {
			ctx.validate(indexRule, ctx.request.query)
			const { count, rows } = await ctx.service.audit.index(
				ctx.request.query
			)
			const data = {
				list: rows,
				total: count,
			}
			ctx.helper.success(data)
		} catch (e) {
			ctx.helper.error(e)
		}
	}

	/**
	 * @summary 审核文章
	 * @description 审核文章
	 * @router put /admin/audit/{id}
	 * @request path string *id 标识
	 * @request body updateAuditRequest
	 */
	async update() {
		const ctx = this.ctx
		try {
			ctx.validate(updateRule, ctx.request.body)
			const { id } = ctx.params
			const status = await ctx.service.audit.update(
				id,
				ctx.request.body
			)
			if (status === 'success') {
				ctx.helper.success('审核成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
}

module.exports = AuditController
